package cas;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author <a href='https://gitee.com/hzf2281'>codefish</a>
 * @version 1.0.0
 * @since 2022/08/20 上午 11:42
 */


public class CasReferenceTest {

    public static void main(String[] args) {
        AtomicReference<User> atomicUser = new AtomicReference<>();
        User u1 = new User("zahngsan", 15);
        User u2 = new User("lisi", 18);
        User u3 = new User("lisi", 18);
        atomicUser.set(u1);
        System.out.println(atomicUser.compareAndSet(u1, u2));
        System.out.println(atomicUser.get());

        System.out.println(atomicUser.compareAndSet(u3, u2));
        System.out.println(atomicUser.get());

    }


    @Data
    @AllArgsConstructor
    static class User {
        String name;
        Integer age;

    }

}
